<%@ page import="java.sql.*" %><%--
  Created by IntelliJ IDEA.
  User: 桔子
  Date: 2018/4/8
  Time: 21:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册账号</title>
</head>
<body>
<%!
    public static final String DBDRIVER="com.mysql.jdbc.Driver";
    public static final String DBURL="jdbc:mysql://youpaiyun.juzibiji.top:3306/test?useSSL=false";
    public static final String DBUSER="juzi";
    public static final String DBPASSWORD="jqsmx1731815301";
%>
<%
    Connection con = null;
    PreparedStatement pstmt1 = null;
    PreparedStatement pstmt2 = null;
    ResultSet rs = null;
    boolean flag=false;
    String name = null;
%>
<%
    try{
        Class.forName(DBDRIVER);
        con=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
        String sql1 = "select userid,name from user";
        pstmt1=con.prepareStatement(sql1);
        rs = pstmt1.executeQuery();
        while (rs.next()){
            if (rs.next()) {
                if (request.getParameter("name").equals(rs.getString(2)) || request.getParameter("id").equals(rs.getString(1))) {
                    flag = false;
                    break;
                } else flag = true;
            }
        }
        if(flag){
            String sql2 = "insert into user (userid,name,password) values (?,?,?)";
            pstmt2=con.prepareStatement(sql2);
            pstmt2.setString(1,request.getParameter("id"));
            pstmt2.setString(2,request.getParameter("name"));
            pstmt2.setString(3,request.getParameter("password"));
            pstmt2.executeUpdate();
        }
    }catch(Exception e){
        System.out.println(e);
    }finally {
        try{
            rs.close();
            pstmt1.close();
            //pstmt2.close();
            con.close();
        }catch (Exception e){
            System.out.println(e);
        }
    }
%>
<%
    if (flag){
        System.out.println(flag);
%>
<jsp:forward page="regist_success.jsp"/>
<%
}else{
    System.out.println(flag);
%>
<jsp:forward page="regist_failure.jsp"/>
<%
    }
%>
</body>
</html>
